package com.ruoyi.cqjd.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.cqjd.domain.CqjdOrderDetail;
import com.ruoyi.cqjd.domain.Page;
import com.ruoyi.cqjd.dto.CqjdOrderDto;
import com.ruoyi.cqjd.service.ICqjdOrderDetailService;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cqjd.domain.CqjdOrders;
import com.ruoyi.cqjd.service.ICqjdOrdersService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单Controller
 * 
 * @author ruoyi
 * @date 2022-06-19
 */
@RestController
@RequestMapping("/cqjd/cqjd_orders")
public class CqjdOrdersController extends BaseController
{
    @Autowired
    private ICqjdOrdersService cqjdOrdersService;


    @Autowired
    private ICqjdOrderDetailService cqjdOrderDetailService;


    @GetMapping("/userPage")
    public R<List<CqjdOrderDto>> userPage(int page, int pageSize,HttpSession session) {

//        构造分页构造器
//        Page pageInfo = new Page(page, pageSize);
////        构造条件构造器
//        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
////
//        queryWrapper.orderByAsc(Orders::getCheckoutTime);
////        查询
//        ordersService.page(pageInfo, queryWrapper);

        Long userId = (Long) session.getAttribute("user");

        List<CqjdOrders> list = cqjdOrdersService.page((page - 1) * pageSize, pageSize, userId);
        List<CqjdOrderDto> records = new ArrayList<>();
        for (CqjdOrders cqjdOrders : list) {
            CqjdOrderDto cqjdOrderDto=new CqjdOrderDto();
            BeanUtils.copyProperties(cqjdOrders,cqjdOrderDto);
            Long orderId = cqjdOrders.getId();
            List<CqjdOrderDetail> orderDetails= cqjdOrderDetailService.getOrderDetailListByOrderId(orderId);
            cqjdOrderDto.setOrderDetails(orderDetails);
            records.add(cqjdOrderDto);
        }
//        Page<List<CqjdOrders>> listPage = new Page<>(records);
        return R.ok(records);
    }



    @PostMapping("/submit")
    public R<String> submit(@RequestBody CqjdOrders orders, HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        cqjdOrdersService.submit(orders,userId);
        return R.ok("订单支付成功");
    }

    /**
     * 查询订单列表
     */
//    @PreAuthorize("@ss.hasPermi('cqjd:cqjd_orders:list')")
    @GetMapping("/list")
    public TableDataInfo list(CqjdOrders cqjdOrders)
    {
        startPage();
        List<CqjdOrders> list = cqjdOrdersService.selectCqjdOrdersList(cqjdOrders);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
//    @PreAuthorize("@ss.hasPermi('cqjd:cqjd_orders:export')")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CqjdOrders cqjdOrders)
    {
        List<CqjdOrders> list = cqjdOrdersService.selectCqjdOrdersList(cqjdOrders);
        ExcelUtil<CqjdOrders> util = new ExcelUtil<CqjdOrders>(CqjdOrders.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
//    @PreAuthorize("@ss.hasPermi('cqjd:cqjd_orders:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(cqjdOrdersService.selectCqjdOrdersById(id));
    }

    /**
     * 新增订单
     */
//    @PreAuthorize("@ss.hasPermi('cqjd:cqjd_orders:add')")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CqjdOrders cqjdOrders)
    {
        return toAjax(cqjdOrdersService.insertCqjdOrders(cqjdOrders));
    }

    /**
     * 修改订单
     */
//    @PreAuthorize("@ss.hasPermi('cqjd:cqjd_orders:edit')")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CqjdOrders cqjdOrders)
    {
        return toAjax(cqjdOrdersService.updateCqjdOrders(cqjdOrders));
    }

    /**
     * 删除订单
     */
//    @PreAuthorize("@ss.hasPermi('cqjd:cqjd_orders:remove')")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cqjdOrdersService.deleteCqjdOrdersByIds(ids));
    }
}
